package financeiro.categoria;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.OrderBy;

import financeiro.usuario.Usuario;

@Entity
public class Categoria implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Integer catCodigo;

	@ManyToOne
	@JoinColumn(name = "catPai", nullable = true)
	@ForeignKey(name = "fk_categoria_categoria")
	private Categoria catPai;

	@ManyToOne
	@JoinColumn(name = "catUsuario")
	@OnDelete(action = OnDeleteAction.CASCADE)
	@ForeignKey(name = "fk_categoria_usuario")
	private Usuario catUsuario;

	@Column(length = 50)
	private String catDescricao;

	private Integer fator;

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
	@JoinColumn(name = "catPai", updatable = false)
	@OrderBy(clause = "catDescricao asc")
	private List<Categoria> catFilhos;
	
	public Categoria() {}
	
	public Categoria( Categoria catPai, Usuario catUsuario,
			String catDescricao, Integer fator) {
		this.catPai = catPai;
		this.catUsuario = catUsuario;
		this.catDescricao = catDescricao;
		this.fator = fator;
	}

	public Integer getCatCodigo() {
		return catCodigo;
	}

	public void setCatCodigo(Integer catCodigo) {
		this.catCodigo = catCodigo;
	}

	public Categoria getCatPai() {
		return catPai;
	}

	public void setCatPai(Categoria catPai) {
		this.catPai = catPai;
	}

	public Usuario getCatUsuario() {
		return catUsuario;
	}

	public void setCatUsuario(Usuario catUsuario) {
		this.catUsuario = catUsuario;
	}

	public String getCatDescricao() {
		return catDescricao;
	}

	public void setCatDescricao(String catDescricao) {
		this.catDescricao = catDescricao;
	}

	public Integer getFator() {
		return fator;
	}

	public void setFator(Integer fator) {
		this.fator = fator;
	}

	public List<Categoria> getCatFilhos() {
		return catFilhos;
	}

	public void setCatFilhos(List<Categoria> catFilhos) {
		this.catFilhos = catFilhos;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((catCodigo == null) ? 0 : catCodigo.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Categoria other = (Categoria) obj;
		if (catCodigo == null) {
			if (other.catCodigo != null)
				return false;
		} else if (!catCodigo.equals(other.catCodigo))
			return false;
		return true;
	}

}
